package com.paso.crmp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.paso.crmp.pojo.CrmpSaleWxrecord;
import com.paso.crmp.pojo.CrmpVipWechat;
import com.paso.crmp.service.ICrmpSaleWxrecordService;
import com.paso.crmp.service.ICrmpVipWechatService;
import com.paso.mycommons.vo.RespBean;
import com.paso.mycommons.vo.RespBeanEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wangx
 * @since 2021-02-18
 */
@RestController
@Slf4j
@RequestMapping("/crmp-sale-wxrecord")
public class CrmpSaleWxrecordController {

    @Resource
    private ICrmpSaleWxrecordService crmpSaleWxrecordService;

    @Resource
    private ICrmpVipWechatService crmpVipWechatService;

    @RequestMapping("/hello")
    public String hell() {
        return "hello";
    }


    /**
     * 订单待上传
     * 根据订单号orderno查询出openid，再根据opnid查询出会员信息
     *
     * @param orderno 订单号
     * @return data里面是List<CrmpVipWechat>
     */
    @PostMapping("/selectVipWechatByorderno")
    public RespBean selectVipWechatByorderno(@RequestParam("orderno") String orderno) {
        // 根据订单查询到openid
        String openid = "";
        try {
            CrmpSaleWxrecord crmpSaleWxrecord = new CrmpSaleWxrecord();
            crmpSaleWxrecord.setOrderno(orderno);
            QueryWrapper<CrmpSaleWxrecord> crmpSaleWxrecordQueryWrapper = new QueryWrapper<>();
            crmpSaleWxrecordQueryWrapper.setEntity(crmpSaleWxrecord);
            CrmpSaleWxrecord saleWxrecord = crmpSaleWxrecordService.getOne(crmpSaleWxrecordQueryWrapper);
            openid = saleWxrecord.getOpenid();
        } catch (Exception e) {
            return RespBean.error(RespBeanEnum.QUERY_OPENID1_FAIL);
//            throw new RuntimeException("根据订单查询openid失败");
        }
        try {
            // 根据opnid查询到VipWechat记录
            CrmpVipWechat crmpVipWechat = new CrmpVipWechat();
            crmpVipWechat.setOpenid(openid);
            List<CrmpVipWechat> list = crmpVipWechatService.list(new QueryWrapper<>(crmpVipWechat));
            return RespBean.success(list);
        } catch (Exception e) {
            return RespBean.error(RespBeanEnum.QUERY_OPENID_FAIL);
//            throw new RuntimeException("根据opnid查询到VipWechat记录失败");
        }

    }


    @RequestMapping(value = "/removeVipWechatById", method = RequestMethod.GET)
    public RespBean removeVipWechatById(@RequestParam(value = "id") String id) {
        try {
			log.info("id= " +id);
            crmpVipWechatService.deleteById(id);
            log.info("删除成功");
            return RespBean.success();
        }catch (Exception e) {
            return RespBean.error(RespBeanEnum.DELETE_VIP_WECHAT_FAIL);
//            throw new RuntimeException("删除VipWechat记录失败");
        }
    }

}
